package com.aikon.wht.controller;

import com.aikon.wht.exception.BaseException;
import com.aikon.wht.properties.OtherProperties;
import com.aikon.wht.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by haitao.wang on 2016/12/9.
 */
@Controller
public class BaseController {

    private static Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    private BaseService baseService;

    /*Another way to autowire*/
//    private final BaseService baseService
//    @Autowired
//    public BaseController(BaseService baseService) {
//        this.baseService = baseService;
//    }

    @Autowired
    private OtherProperties otherProperties;

    @RequestMapping("/")
    @ResponseBody
    public String getBaseServiceName() {
        return baseService.serviceName;
    }

    @Value("${my.uuid}")
    private String uuid;

    @Value("${my.number.in.range}")
    private String intRange;

    @RequestMapping("/randomValue")
    @ResponseBody
    public String getRandomValue(){
        return uuid + intRange;
    }

    @RequestMapping("/test")
    public String getTest() {
        return "test";
    }

    @RequestMapping("/p")
    @ResponseBody
    public String getP() {
        logger.info("pppppppppppp");
        return otherProperties.getAddress();
    }

    @RequestMapping("/exception")
    @ResponseBody
    public String getException() throws BaseException {
        if (1 == 1)
            throw new BaseException();
        return "no exception";
    }

    @RequestMapping("/myError")
    public String getError() {
        return "myError";
    }

    @RequestMapping("/redirect")
    public void redirect(HttpServletResponse response) {
        try {
            response.sendRedirect("/test");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
